<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class Food
 * @package App\Models
 */
class Food extends Model
{
    const FORBIDDEN = 0;        // 禁用
    const START_USING = 1;      // 启用
    const SUPPLY_TYPES = [1, 2, 3];     // 供应的早，中，晚餐

    protected $fillable = [
        'name',
        'description',
        'supply_type',
        'image',
        'thumb_image',
        'status',
        'food_type_id',
        'school_id'
    ];

    /**
     * @param $query
     * @param $name
     */
    public function scopeNameLike($query, $name)
    {
        $query->when($name, function ($query) use ($name) {
            $query->where('name', 'like', $name . '%');
        });
    }

    /**
     * @param $query
     * @param $supplyType
     * 筛选供应类型
     */
    public function scopeSupplyTypeIs($query, $supplyType)
    {
        $query->when($supplyType, function ($query) use ($supplyType) {
            if (count($supplyType) === 1) {
                if ($supplyType[0] == 1) {
                    $query->whereIn('supply_type', [1, 12, 13, 123]);
                } elseif ($supplyType[0] == 2) {
                    $query->whereIn('supply_type', [2, 12, 23, 123]);
                } elseif ($supplyType[0] == 3) {
                    $query->whereIn('supply_type', [3, 13, 23, 123]);
                }
            } elseif (count($supplyType) === 3) {
                $query->where('supply_type', 123);
            } elseif (count($supplyType) === 2) {
                if (!array_diff($supplyType, [1, 2])) {
                    $query->whereIn('supply_type', [12, 123]);
                }
                if (!array_diff($supplyType, [1, 3])) {
                    $query->whereIn('supply_type', [13, 123]);
                }
                if (!array_diff($supplyType, [2, 3])) {
                    $query->whereIn('supply_type', [23, 123]);
                }
            }
        });
    }

    /**
     * @param $query
     * @param $status
     */
    public function scopeStatusIs($query, $status)
    {
        $query->when($status, function ($query) use ($status) {
            $query->where('status', $status);
        });
    }

    /**
     * @param $query
     * @param $foodType
     */
    public function scopeFoodTypeIs($query, $foodType)
    {
        $query->when($foodType, function ($query) use ($foodType) {
            $query->where('food_type_id', $foodType);
        });
    }

    /**
     * @param $query
     * @param $schoolId
     */
    public function scopeSchoolIs($query, $schoolId)
    {
        $query->when($schoolId, function ($query) use ($schoolId) {
            $query->where('school_id', $schoolId);
        });
    }

    /**
     * @param $value
     * 重组菜品供应类型
     */
    public function setSupplyTypeAttribute($value)
    {
        if (count($value) === 1) {
            $this->attributes['supply_type'] = $value[0];
        } elseif (count($value) === 3) {
            $this->attributes['supply_type'] = 123;
        } elseif (count($value) === 2) {
            if (!array_diff($value, [1, 2])) {
                $this->attributes['supply_type'] = 12;
            }
            if (!array_diff($value, [1, 3])) {
                $this->attributes['supply_type'] = 13;
            }
            if (!array_diff($value, [2, 3])) {
                $this->attributes['supply_type'] = 23;
            }
        }
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function school()
    {
        return $this->belongsTo(School::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function type()
    {
        return $this->belongsTo(FoodType::class, 'food_type_id', 'id');
    }

    /**
     * @return string
     */
    public function getFullThumbImagesAttribute()
    {
        return (file_exists(public_path().$this->image) && !empty($this->image)) ? asset($this->attributes['thumb_image']) : asset('/images/default.png');
    }
}
